package com.sufen.seckill.study.FaceCoding;

import java.util.concurrent.*;

public class ThreadPoolExecutorHelp {
    public static void main(String[] args) {
//        Runnable r=new Print50();

//        ThreadPoolExecutor threadPoolExecutor=new ThreadPoolExecutor(2,2,0,TimeUnit.SECONDS,new LinkedBlockingQueue<>());
//        threadPoolExecutor.execute(r1);
//        threadPoolExecutor.execute(r2);

//        Thread t1=new Thread(r);
//        Thread t2=new Thread(r);
//        t1.start();
//        t2.start();

        Callable<Integer> c=new Print51();
        FutureTask<Integer> f=new FutureTask<Integer>(c);
        Thread t1=new Thread(f);
        Thread t2=new Thread(f);
        t1.start();
        t2.start();
        try {
            System.out.println(Thread.currentThread().getName()+":"+f.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }

}

class Print51 implements Callable<Integer>{
    private int num=0;
    @Override
    public Integer call() throws Exception {
        while (num < 100) {
            synchronized (this) {
                num++;
                System.out.println(Thread.currentThread().getName()+":"+num);
//                notify();
//                wait();
//                notify();
            }
        }
        return num;
    }
}

class Print50 implements Runnable{

    private int num=0;
    @Override
    public void run() {
        while (num < 100) {

            synchronized (this) {
                num++;
                System.out.println(num+Thread.currentThread().getName());
                notify();
                try {
                    wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                notify();
            }
        }
    }
//    private int i = 0;
//    @Override
//    public void run() {
//        while (true){
//            synchronized (this) {
//                notify();
//                if(i < 100){
//                    i++;
//                    System.out.println(Thread.currentThread().getName()+"---"+i);
//                }else{
//                    break;
//                }
//                try {
//                    wait();
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            }
//        }
//    }
}
